import { cloneDeep } from 'lodash';

import menuItemPool from "src/api/config/menuItemPool.json";
import { GraphShapeUtil } from './GraphShapeUtil';

export class ContextMenuUtil {
  graph: Graph;
  constructor(graph: Graph) {
    this.graph = graph;
  }
  /**
   * 获取图形的右键菜单
   * @param shape
   * @param diagramType
   */
  static getShapeMenu(shape: UmlShape, diagramType: string): TreeNode[] {

    const diagramConfig = app.activeProject.configManager.getShapeContextMenuConfig()[diagramType];
    const typeName = GraphShapeUtil.parseStName(shape.getTypeName());
    console.log('diagramConfig', diagramConfig, typeName, diagramType);
    const menuItems: Array<string> = diagramConfig[typeName];
    if (!menuItems) return [];
    const hiddenDisplayRelated = {
      Instance: {
        diagramType: 'all',
        showRelationEdge: true,
        showRelatedElement: true,
      },
      Property: {
        diagramType: ['SysMLBlockDefinitionDiagram', 'SysMLPackageDiagram'],
        showRelationEdge: true,
        showRelatedElement: true,
      },
      Port: {
        diagramType: 'all',
        showRelationEdge: true,
        showRelatedElement: true,
        showInternalPort: true
      },
      //// Block: {
      ////   diagramType: 'all',
      ////   showRelationEdge: true,
      ////   showRelatedElement: true,
      ////   showInternalPort: true
      //// },
    };
    let result = menuItems.map((name: string) => menuItemPool[name]).filter(Boolean);
    result = cloneDeep(result);

    result.forEach(item => {
      if (item.value === 'navTo'){
        item.children = this.getNavToMenu(shape.container);
      }
      const displayRelatedTypeName = Object.keys(hiddenDisplayRelated).find(key => typeName.includes(key));
      const currentDisplayRelated = hiddenDisplayRelated[displayRelatedTypeName];
      if (
        item.value === 'displayRelated' 
        && currentDisplayRelated
        && (currentDisplayRelated.diagramType === 'all' 
        || currentDisplayRelated.diagramType.includes(diagramType))
      ) { //// 控制菜单的显示选项
        item.children = item.children.filter(child => currentDisplayRelated[child.value]);
      }

    });

    if (shape.container.disabled) {
      result = result.map(i => ({ ...i, disabled: i.disabledWhenReadonly }));
    }
    console.log('result', result);
    return result;
  }
  static getNavToMenu(graphNode:GraphNode):TreeNode[]{
    return graphNode.getLinks().map(link => link.getDiagram()).map(diagramNode => ({
      label: diagramNode.title,
      value: 'navigation',
      graphNode: diagramNode,
      icon: diagramNode.iconPath
    }));

  }

}
